home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / PARAGRAP.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  8KB  |  295 lines

  1. #include "jam.h"
  2. /*
  3.  * Code for dealing with paragraphs and filling. Adapted from MicroEMACS 3.6
  4.  * and GNU-ified by mwm@ucbvax.     Several bug fixes by blarson@usc-oberon.
  5.  */
  6. #include "def.h"
  7.  
  8. static int    fillcol = 70 ;
  9. #define MAXWORDS 256
  10.  
  11. /*
  12.  * go back to the begining of the current paragraph
  13.  * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  14.  * combination to delimit the begining of a paragraph
  15.  */
  16. /*ARGSUSED*/
  17. gotobop(f, n)
  18. int f, n;
  19. {
  20.     if (n < 0)    /* the other way...*/
  21.         return gotoeop(f, -n);
  22.  
  23.     while (n-- > 0) {    /* for each one asked for */
  24.  
  25.         /* first scan back until we are in a word */
  26.         
  27.         while (backchar(FFRAND, 1) && !inword()) {}
  28.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  29.  
  30.         /* and scan back until we hit a <NL><SP> <NL><TAB> or <NL><NL> */
  31.         while (lback(curwp->w_dotp) != curbp->b_linep)
  32.             if (llength(lback(curwp->w_dotp))
  33.                 && lgetc(curwp->w_dotp,0) != ' '
  34.                 && lgetc(curwp->w_dotp,0) != '\t')
  35.                 curwp->w_dotp = lback(curwp->w_dotp);
  36.             else
  37.                 break;
  38.     }
  39.     curwp->w_flag |= WFMOVE;    /* force screen update */
  40.     return TRUE;
  41. }
  42.  
  43. /*
  44.  * go forword to the end of the current paragraph
  45.  * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  46.  * combination to delimit the begining of a paragraph
  47.  */
  48. /*ARGSUSED*/
  49. gotoeop(f, n)
  50. int f, n;
  51. {
  52.     if (n < 0)    /* the other way...*/
  53.         return gotobop(f, -n);
  54.  
  55.     while (n-- > 0) {    /* for each one asked for */
  56.  
  57.         /* Find the first word on/after the current line */
  58.         curwp->w_doto = 0;
  59.         while(forwchar(FFRAND, 1) && !inword()) {}
  60.         curwp->w_doto = 0;
  61.         curwp->w_dotp = lforw(curwp->w_dotp);
  62.         /* and scan forword until we hit a <NL><SP> or ... */
  63.         while (curwp->w_dotp != curbp->b_linep) {
  64.             if (llength(curwp->w_dotp)
  65.                 && lgetc(curwp->w_dotp,0) != ' '
  66.                 && lgetc(curwp->w_dotp,0) != '\t')
  67.                 curwp->w_dotp = lforw(curwp->w_dotp);
  68.             else
  69.                 break;
  70.         }
  71.         if(curwp->w_dotp == curbp->b_linep) {
  72.             /* beond end of buffer, cleanup time */
  73.             curwp->w_dotp = lback(curwp->w_dotp);
  74.             curwp->w_doto = llength(curwp->w_dotp);
  75.             break;            
  76.         }
  77.     }
  78.     curwp->w_flag |= WFMOVE;    /* force screen update */
  79.     return TRUE;
  80. }
  81.  
  82. /*
  83.  * Fill the current paragraph according to the current
  84.  * fill column
  85.  */
  86. /*ARGSUSED*/
  87. fillpara(f, n)
  88. int f, n;
  89. {
  90.     register int    c;        /* current char durring scan    */
  91.     register int    wordlen;    /* length of current word    */
  92.     register int    clength;    /* position on line during fill */
  93.     register int    i;        /* index during word copy    */
  94.     register int    eopflag;    /* Are we at the End-Of-Paragraph? */
  95.     int        firstflag;    /* first word? (needs no space) */
  96.     int        newlength;    /* tentative new line length    */
  97.     int        eolflag;    /* was at end of line        */
  98.     LINE        *eopline;    /* pointer to line just past EOP */
  99.     char wbuf[MAXWORDS];        /* buffer for current word    */
  100.  
  101.   if (curbp->b_flag & BFVIEW)
  102.     {
  103.       ttbeep();
  104.       return FALSE;
  105.     }
  106.   if (filetimechanged(curbp))
  107.     return(FALSE);
  108.   clearUndo(curbp);
  109.  
  110.     /* record the pointer to the line just past the EOP */
  111.     (VOID) gotoeop(FFRAND, 1);
  112.     if(curwp->w_doto != 0)    {
  113.         /* paragraph ends at end of buffer */
  114.         (VOID) lnewline();
  115.         eopline = lforw(curwp->w_dotp);
  116.     } else    eopline = curwp->w_dotp;
  117.  
  118.     /* and back top the begining of the paragraph */
  119.     (VOID) gotobop(FFRAND, 1);
  120.  
  121.     /* initialize various info */
  122.     while (!inword() && forwchar(FFRAND, 1)) {}
  123.     clength = curwp->w_doto;
  124.     wordlen = 0;
  125.  
  126.     /* scan through lines, filling words */
  127.     firstflag = TRUE;
  128.     eopflag = FALSE;
  129.     while (!eopflag) {
  130.         /* get the next character in the paragraph */
  131.         if (eolflag=(curwp->w_doto == llength(curwp->w_dotp))) {
  132.             c = ' ';
  133.             if (lforw(curwp->w_dotp) == eopline)
  134.                 eopflag = TRUE;
  135.         } else
  136.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  137.  
  138.         /* and then delete it */
  139.         if (ldelete((RSIZE) 1, KNONE) == FALSE && !eopflag)
  140.             return FALSE;
  141.  
  142.         /* if not a separator, just add it in */
  143.         if (c != ' ' && c != '\t') {
  144.             if (wordlen < MAXWORDS - 1)
  145.                 wbuf[wordlen++] = (char)c;
  146.             else {
  147.                 /* You loose chars beyond MAXWORDS if the word
  148.                  * is to long. I'm to lazy to fix it now; it
  149.                  * just silently truncated the word before, so
  150.                  * I get to feel smug.
  151.                  */
  152.                 ewprintf("Word too long!");
  153.             }
  154.         } else if (wordlen) {
  155.             /* calculate tenatitive new length with word added */
  156.             newlength = clength + 1 + wordlen;
  157.             /* if at end of line or at doublespace and previous
  158.              * character was one of '.','?','!' doublespace here.
  159.              */
  160.             if((eolflag || curwp->w_doto==llength(curwp->w_dotp)
  161.                 || (c=lgetc(curwp->w_dotp,curwp->w_doto))==' '
  162.                 || c=='\t')
  163.               && ISEOSP(wbuf[wordlen-1])
  164.               && wordlen<MAXWORDS-1)
  165.                 wbuf[wordlen++] = ' ';
  166.             /* at a word break with a word waiting */
  167.             if (newlength <= fillcol) {
  168.                 /* add word to current line */
  169.                 if (!firstflag) {
  170.                     (VOID) linsert(1, ' ');
  171.                     ++clength;
  172.                 }
  173.                 firstflag = FALSE;
  174.             } else {
  175.                 if(curwp->w_doto > 0 &&
  176.                     lgetc(curwp->w_dotp,curwp->w_doto-1)==' ') {
  177.                     curwp->w_doto -= 1;
  178.                     (VOID) ldelete((RSIZE) 1, KNONE);
  179.                 }
  180.                 /* start a new line */
  181.                 (VOID) lnewline();
  182.                 clength = 0;
  183.             }
  184.  
  185.             /* and add the word in in either case */
  186.             for (i=0; i<wordlen; i++) {
  187.                 (VOID) linsert(1, wbuf[i]);
  188.                 ++clength;
  189.             }
  190.             wordlen = 0;
  191.         }
  192.     }
  193.     /* and add a last newline for the end of our new paragraph */
  194.     (VOID) lnewline();
  195.     /* we realy should wind up where we started, (which is hard to keep
  196.      * track of) but I think the end of the last line is better than the
  197.      * begining of the blank line.     */
  198.     (VOID) backchar(FFRAND, 1);
  199.     return TRUE;
  200. }
  201.  
  202. /* delete n paragraphs starting with the current one */
  203. /*ARGSUSED*/
  204. killpara(f, n)
  205. int f, n;
  206. {
  207.   register int status;    /* returned status of functions */
  208.  
  209.   if (curbp->b_flag & BFVIEW)
  210.     {
  211.       ttbeep();
  212.       return FALSE;
  213.     }
  214.     while (n--) {        /* for each paragraph to delete */
  215.  
  216.         /* mark out the end and begining of the para to delete */
  217.         (VOID) gotoeop(FFRAND, 1);
  218.  
  219.         /* set the mark here */
  220.         curwp->w_markp = curwp->w_dotp;
  221.         curwp->w_marko = curwp->w_doto;
  222.  
  223.         /* go to the begining of the paragraph */
  224.         (VOID) gotobop(FFRAND, 1);
  225.         curwp->w_doto = 0;    /* force us to the begining of line */
  226.  
  227.         /* and delete it */
  228.         if ((status = killregion(FFRAND, 1)) != TRUE)
  229.             return status;
  230.  
  231.         /* and clean up the 2 extra lines */
  232.         (VOID) ldelete((RSIZE) 1, KFORW);
  233.     }
  234.     return TRUE;
  235. }
  236.  
  237. /*
  238.  * check to see if we're past fillcol, and if so,
  239.  * justify this line. As a last step, justify the line.
  240.  */
  241. /*ARGSUSED*/
  242. fillword(f, n)
  243. int f, n;
  244. {
  245.     register char    c;
  246.     register int    col, i, nce;
  247.  
  248.   if (curbp->b_flag & BFVIEW)
  249.     {
  250.       ttbeep();
  251.       return FALSE;
  252.     }
  253.  
  254.     for (i = col = 0; col <= fillcol; ++i, ++col) {
  255.         if (i == curwp->w_doto) return selfinsert(f, n) ;
  256.         c = lgetc(curwp->w_dotp, i);
  257.         if (c == '\t') col |= TABROUND;
  258.         else if (ISCTRL(c) != FALSE) ++col;
  259.     }
  260.     if (curwp->w_doto != llength(curwp->w_dotp)) {
  261.         (VOID) selfinsert(f, n);
  262.         nce = llength(curwp->w_dotp) - curwp->w_doto;
  263.     } else nce = 0;
  264.     curwp->w_doto = i;
  265.  
  266.     if ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' && c != '\t')
  267.         do {
  268.             (VOID) backchar(FFRAND, 1);
  269.         } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' '
  270.               && c != '\t' && curwp->w_doto > 0);
  271.  
  272.     if (curwp->w_doto == 0)
  273.         do {
  274.             (VOID) forwchar(FFRAND, 1);
  275.         } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' '
  276.               && c != '\t' && curwp->w_doto < llength(curwp->w_dotp));
  277.  
  278.     (VOID) delwhite(FFRAND, 1);
  279.     (VOID) lnewline();
  280.     i = llength(curwp->w_dotp) - nce;
  281.     curwp->w_doto = i>0 ? i : 0;
  282.     curwp->w_flag |= WFMOVE;
  283.     if (nce == 0 && curwp->w_doto != 0) return fillword(f, n);
  284.     return TRUE;
  285. }
  286.  
  287. /* Set fill column to n. */
  288. setfillcol(f, n) 
  289. int f, n;
  290. {
  291.   fillcol =((f & FFARG) ? n :getcolpos());
  292.   ewprintf("Fill column set to %d", fillcol);
  293.   return TRUE;
  294. }
  295.